//
//  BanBu_MagicFaceCell.m
//  BanBu
//
//  Created by Wang Xj on 13-10-29.
//
//

#import "BanBu_MagicFaceCell.h"
#import "BanBu_ImageView.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"

@implementation BanBu_MagicFaceCell

@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 89, 320, 1.0)];
        lineView.backgroundColor = [UIColor colorWithRed:224.0/255 green:224.0/255 blue:224.0/255 alpha:1.0];
        [self.contentView addSubview:lineView];
        [lineView release];
        
        BanBu_ImageView *backImage = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeCell];
        backImage.frame = CGRectMake(0, 0, 320, 88);
        self.backgroundView = backImage;
        [backImage release];
        
        self.firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 67.5, 67.5)];
        self.secondImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10+67.5+10, 10, 67.5, 67.5)];
        self.thirdImageView =[[UIImageView alloc]initWithFrame:CGRectMake((10 +67.5)*2+10, 10, 67.5, 67.5)];
        self.fourthImageView =[[UIImageView alloc]initWithFrame:CGRectMake((10 +67.5)*3+10, 10, 67.5, 67.5)];
        
        [self.contentView addSubview:self.firstImageView];
        [self.contentView addSubview:self.secondImageView];
        [self.contentView addSubview:self.thirdImageView];
        [self.contentView addSubview:self.fourthImageView];
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)clearImage
{
    self.firstImageView.image = nil;
    self.secondImageView.image = nil;
    self.thirdImageView.image = nil;
    self.fourthImageView.image = nil;
}

-(void)addGesture
{
    //添加tag值
    self.firstImageView.tag = 1;
    self.firstImageView.userInteractionEnabled = YES;
    self.secondImageView.tag = 2;
    self.secondImageView.userInteractionEnabled = YES;
    self.thirdImageView.tag = 3;
    self.thirdImageView.userInteractionEnabled = YES;
    self.fourthImageView.tag = 4;
    self.fourthImageView.userInteractionEnabled = YES;
    
    //添加单击手势
    UITapGestureRecognizer * firstTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [self.firstImageView addGestureRecognizer:firstTap];
    [firstTap release];
    UITapGestureRecognizer * secondTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [self.secondImageView addGestureRecognizer:secondTap];
    [secondTap release];
    UITapGestureRecognizer * thirdTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [self.thirdImageView addGestureRecognizer:thirdTap];
    [thirdTap release];
    UITapGestureRecognizer * fourthTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [self.fourthImageView addGestureRecognizer:fourthTap];
    [fourthTap release];

}

-(void)setImageUrl:(NSArray *)imagrUrlArray
{
    NSInteger index = 0;
    for (int i = 0; i<4 && i<imagrUrlArray.count;i++)
    {
        NSDictionary * Dic = [imagrUrlArray objectAtIndex:i];
        NSString * imageUrl = [Dic valueForKey:@"small"];
        switch (i)
        {
            case 0:
                self.firstBigGifUrl = [Dic valueForKey:@"big"];
                self.firstSmallGifUrl = imageUrl;
                break;
            case 1:
                self.secondBigGifUrl = [Dic valueForKey:@"big"];
                self.secondSmallGifUrl = imageUrl;
                break;
            case 2:
                self.thirdBigGifUrl = [Dic valueForKey:@"big"];
                self.thirdSmallGifUrl = imageUrl;
                break;
            case 3:
                self.fourthBigGifUrl = [Dic valueForKey:@"big"];
                self.fourthSmallGifUrl = imageUrl;
                break;
                
            default:
                break;
        }
        
        
        [self performSelectorInBackground:@selector(downloadImage:) withObject:@[imageUrl,[NSString stringWithFormat:@"%d",index]]];
        index++;
    }
}

-(void)setDownloadImage:(NSArray *)imageInfo
{
    NSData * imageData = [imageInfo objectAtIndex:0];
    NSInteger index = [[imageInfo objectAtIndex:1] integerValue];
    
    switch (index)
    {
        case 0:
            self.firstImageView.image = [UIImage imageWithData:imageData];
            break;
        case 1:
            self.secondImageView.image = [UIImage imageWithData:imageData];
            break;
        case 2:
            self.thirdImageView.image = [UIImage imageWithData:imageData];
            break;
        case 3:
            self.fourthImageView.image = [UIImage imageWithData:imageData];
            break;
        default:
            break;
    }
}

-(void)downloadImage:(NSArray *)imageInfo
{
    NSString * imageUrl = [imageInfo objectAtIndex:0];
    NSInteger index = [[imageInfo objectAtIndex:1] integerValue];
    NSData * imageData = nil;
    //查看是否已下载过
    if(![self checkDownloaded:imageInfo])
    {
        imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        if (imageData == nil)
        {
            return;
        }
        [self saveImage:@[imageData,imageUrl]];
    }
    else
    {
        imageData  = [MyAppDataManager msgimageDataForImageUrlStr:imageUrl];
    }
    [self performSelectorOnMainThread:@selector(setDownloadImage:) withObject:@[imageData,[NSString stringWithFormat:@"%d",index]] waitUntilDone:YES];
    
}

//检查图片是否已下载：YES：已下载；NO：未下载
-(BOOL)checkDownloaded:(NSArray *)imageInfo
{
    NSString * imageUrl = [imageInfo objectAtIndex:0];
    
    NSData * imageData = [MyAppDataManager msgimageDataForImageUrlStr:imageUrl];
    
    UIImage * image = [UIImage imageWithData:imageData];
    
    if (image == nil)
    {
        return NO;
    }
    return YES;
}

-(void)saveImage:(NSArray *)imageInfo
{
    NSData * imageData = [imageInfo objectAtIndex:0];
    NSString * filePath  =[AppComManager pathForMsgMedia:[imageInfo objectAtIndex:1]];
    [imageData writeToFile:filePath atomically:YES];
}

-(void)singleTap:(UITapGestureRecognizer *)tap
{
    if ([_delegate respondsToSelector:@selector(menuShow:tableCell:)])
    {
        [_delegate menuShow:tap.view tableCell:self];
    }    
}

-(void)dealloc
{
    [_firstImageView release],_firstImageView = nil;
    [_secondImageView release],_secondImageView = nil;
    [_thirdImageView release],_thirdImageView = nil;
    [_fourthImageView release],_fourthImageView = nil;
    
    [_firstBigGifUrl release],_firstBigGifUrl = nil;
    [_secondBigGifUrl release],_secondBigGifUrl = nil;
    [_thirdBigGifUrl release],_thirdBigGifUrl = nil;
    [_fourthBigGifUrl release], _fourthBigGifUrl = nil;

    [_firstSmallGifUrl release],_firstSmallGifUrl = nil;
    [_secondSmallGifUrl release],_secondSmallGifUrl = nil;
    [_thirdSmallGifUrl release],_thirdSmallGifUrl = nil;
    [_fourthSmallGifUrl release],_fourthSmallGifUrl = nil;
    
    [super dealloc];
}

@end
